import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

export default new Vuex.Store({
  plugins: [
    createPersistedState({
      storage: window.sessionStorage, // 默认位置是 localStorage
      reducer: (state) => {
        return {
          // 默认是全部缓存，在这里可以设置需要缓存的状态
          token: state.token,
          userInfo: state.userInfo,
        };
      },
    }),
  ],
  state: {
    tagNames: [],
    //登录
    userInfo: {},
    token: '',
    dynamicRoutes: [],
  },
  getters: {},
  mutations: {
    //添加tag标签
    addTag(state, tagName) {
      let status = true;
      state.tagNames.forEach((val) => {
        if (val.name === tagName.name) {
          status = false;
        }
      });
      if (status === true) {
        state.tagNames.push(tagName);
      }
    },
  },
  actions: {},
  modules: {},
});
